/**
 * This file is part of JNotifyOSD.
 *
 * JNotifyOSD is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JNotifyOSD is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JNotifyOSD. If not, see <http://www.gnu.org/licenses/>.
 */

package notifyosd;

import com.sun.istack.internal.logging.Logger;
import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.ref.SoftReference;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * Abstract factory for {@link Notification} objects.
 * @author Giulio Franco <g.franco9@studenti.unisa.it>
 * @ThreadSafe
 */
public abstract class NotificationService {
    private static SoftReference<NotificationService> instance
            = new SoftReference<NotificationService>(null);
    
    /**
     * Returns an instance of NotificationService.
     * @return NotificationService
     */
    public static synchronized NotificationService getInstance() {
        NotificationService ns = instance.get();
        if (ns == null) {
            try {
                ns = new NativeNotificationService();
            } catch (UnsatisfiedLinkError err) {
                Logger.getLogger(NotificationService.class).warning(
                        "Unable to link the JNotify library. "
                        + "Falling back to pure Java.", err);
                ns = new JavaNotificationService();
            }
            
            instance = new SoftReference<NotificationService>(ns);
        }
        return ns;
    }
        
    /**
     * Returns a set of {@link String}s,
     * describing the capabilities of this service.
     * 
     * @return Set of capability strings.
     */
    public abstract Set<String> getServerCaps();
    
    /**
     * Returns information about the notification server.
     * @return {@link ServerInformation} object
     */
    public abstract ServerInformation getServerInfo();
    
    /**
     * Returns the application name
     * that will be used to communicate with the notification server.
     * @return Application name
     */
    public abstract String applicationName();
    
    /**
     * Creates a new Notification object
     * @param title Title of the notification
     * @return New notification
     */
    public Notification newNotification(String title) {
        return newNotification(title, null, (String)null);
    }
    
    /**
     * Creates a new Notification object
     * @param title Title of the notification
     * @param body  Text of the notification
     * @return New notification
     */
    public Notification newNotification(String title, String body) {
        return newNotification(title, body, (String)null);
    }
    
    /**
     * Creates a new Notification object
     * @param title Title of the notification
     * @param body  Text of the notification
     * @param icon  Path of the icon that will be displayed in the notification
     * @return New notification
     */
    public abstract Notification newNotification(String title, String body, String icon);
    
    /**
     * Creates a new Notification object
     * @param title Title of the notification
     * @param body  Text of the notification
     * @param icon  Icon that will be displayed in the notification
     * @return New notification
     */
    public Notification newNotification(String title, String body, File icon) {
        return newNotification(title, body, icon.getAbsolutePath());
    }
    
    /**
     * Creates a new Notification object
     * @param title Title of the notification
     * @param icon  Icon that will be displayed in the notification
     * @return New notification
     */
    public Notification newNotification(String title, File icon) {
        return newNotification(title, null, icon.getAbsolutePath());
    }
}
